package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.exception.GlobalExceptionHandler;
import com.atguigu.commonutils.exception.GuliException;
import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.Teacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ran
 * @since 2022-01-25
 */
@Api("讲师管理")
@CrossOrigin//解决跨域问题
@RestController
@RequestMapping("/eduservice/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R list(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }


    @ApiOperation("根据id获取讲师")
    @GetMapping("/{id}")
    public R getTeacher(@PathVariable("id") String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("/delete/{id}")
    public R deleteTea(@PathVariable("id")String id){
        String s = String.valueOf(id);
        boolean b = teacherService.removeById(s);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("分页获取讲师")
    @GetMapping("getTeacher/{page}/{limit}")
    public R getTeacherPage(@PathVariable("page") int page,
                            @PathVariable("limit") int limit){

        Page<Teacher> teacherPage = new Page<>(page,limit);
        IPage<Teacher> teacherIPage = teacherService.page(teacherPage, null);
        long total = teacherIPage.getTotal();
        List<Teacher> teachers = teacherIPage.getRecords();
        return R.ok().data("total",total).data("rows",teachers);
    }

    @PostMapping("{page}/{limit}")
    public R teacherPageQuery(@PathVariable(value = "page",required = true)int page,
                              @PathVariable(value = "limit",required = true)int limit,
                              @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<Teacher> pageParam = new Page<>(page, limit);
        Map map = teacherService.pageQuery(pageParam, teacherQuery);
        List items = (List) map.get("items");
        Long total = (Long) map.get("total");
        return R.ok().data("rows",items).data("total",total);
    }
    //添加讲师接口的方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false)  TeacherQuery teacherQuery) {
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        wrapper.orderByDesc("gmtCreate");
        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<Teacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

}

